package 反射;

import javax.swing.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class MyReflectText1 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        /**
         * 反射的运用场景2
         * 反射可以跟配置文件结合的方式，动态的创建对象，并调用方法
         */
        //1.获取配置文件
        Properties prop=new Properties();
        //读取本地文件
        FileInputStream is=new FileInputStream("D:\\idea2022\\day.com.qiezi\\day_15\\src\\反射\\peop.properties");
        prop.load(is);
        is.close();
        System.out.println(prop);

        //获取全类名,和方法名
        String classname = (String) prop.get("classname");
        String method = (String) prop.get("method");
        System.out.println(classname);
        System.out.println(method);

        //获取成功,利用反射创建对象并运行方法
        Class aClass = Class.forName(classname);

        //获取构造方法
        Constructor con = aClass.getDeclaredConstructor();
        Object o = con.newInstance();
        System.out.println(o);

        //获取成员方法并运行
        Method method1 = aClass.getDeclaredMethod("method");
        //临时取消访问权限
        method1.setAccessible(true);
        method1.invoke(o);

    }
}
class Teacher{
    private String name;
    private int age;

    public Teacher() {
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Teacher{name = " + name + ", age = " + age + "}";
    }
    public void teach(String name){
        System.out.println(name + "老师正在教书");
    }
}
class Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Student{name = " + name + ", age = " + age + "}";
    }
    public void teach(String name){
        System.out.println(name + "学生正在学习");
    }
}
